// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

package com.compass.qihuang;


import android.content.Context;
import android.graphics.Color;
import android.os.Environment;
import android.text.TextUtils;

import com.compass.doctor.ui.MainActivity;
import com.compass.qihuang.push.PushUserInfoProvider;
import com.netease.nimlib.sdk.NotificationFoldStyle;
import com.netease.nimlib.sdk.SDKOptions;
import com.netease.nimlib.sdk.StatusBarNotificationConfig;
import com.netease.nimlib.sdk.StatusBarNotificationFilter;
import com.netease.nimlib.sdk.mixpush.MixPushConfig;
import com.netease.yunxin.kit.common.utils.ScreenUtils;

import java.io.IOException;

/**
 * Nim SDK config info
 */
public class NimSDKOptionConfig {


    public static final int LED_ON_MS = 1000;
    public static final int LED_OFF_MS = 1500;

    static SDKOptions getSDKOptions(Context context, String appKey) {
        SDKOptions options = new SDKOptions();
        options.appKey = appKey;
        initStatusBarNotificationConfig(options);
        options.sdkStorageRootPath = getAppCacheDir(context);
        options.preloadAttach = true;
        options.disableAwake=true;
        options.thumbnailSize = (int) (222.0 / 375.0 * ScreenUtils.getDisplayWidth());
        options.userInfoProvider = new PushUserInfoProvider(context);
        options.sessionReadAck = true;
        options.animatedImageThumbnailEnabled = true;
        options.asyncInitSDK = true;
        options.reducedIM = false;
        //是否在 SDK 初始化时检查 Manifest 文件配置是否完成，默认为 false，不检查。
        //par使用建议： 建议在调试阶段开启，上线时关闭
        options.checkManifestConfig = true;
        options.enableTeamMsgAck = true;
        options.enableFcs = false;
        // 打开消息撤回未读数-1的开关
        options.shouldConsiderRevokedMessageUnreadCount = true;
        // 会话置顶是否漫游
        options.notifyStickTopSession = true;
        options.mixPushConfig = buildMixPushConfig();

        return options;
    }


    public static void initStatusBarNotificationConfig(SDKOptions options) {
        // load notification
        StatusBarNotificationConfig config = loadStatusBarNotificationConfig();
        // load 用户的 StatusBarNotificationConfig 设置项
        // SDK statusBarNotificationConfig 生效

        config.notificationFilter = imMessage ->
                        CompassApplication.getForegroundActCount() > 0
                                ? StatusBarNotificationFilter.FilterPolicy.DENY
                                : StatusBarNotificationFilter.FilterPolicy.DEFAULT;
        options.statusBarNotificationConfig = config;
    }

    // config StatusBarNotificationConfig
    public static StatusBarNotificationConfig loadStatusBarNotificationConfig() {
        StatusBarNotificationConfig config = new StatusBarNotificationConfig();
        // 点击通知需要跳转到的界面
        config.notificationEntrance = MainActivity.class;
        config.notificationSmallIconId =com.compass.doctor.R.drawable.ic_notification_logo;
        config.notificationColor = Color.parseColor("#3a9efb");
        config.notificationFoldStyle = NotificationFoldStyle.ALL;
        config.downTimeEnableNotification = true;
        // 呼吸灯配置
        config.ledARGB = Color.GREEN;
        config.ledOnMs = LED_ON_MS;
        config.ledOffMs = LED_OFF_MS;
        // 是否APP ICON显示未读数红点(Android O有效)
        config.showBadge = true;
        return config;
    }

    /**
     * config app image/voice/file/log directory
     */
    static String getAppCacheDir(Context context) {
        String storageRootPath = null;
        try {
            if (context.getExternalCacheDir() != null) {
                storageRootPath = context.getExternalCacheDir().getCanonicalPath();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (TextUtils.isEmpty(storageRootPath)) {
            storageRootPath = Environment.getExternalStorageDirectory() + "/" + context.getPackageName();
        }
        return storageRootPath;
    }

    private static MixPushConfig buildMixPushConfig() {
        MixPushConfig config = new MixPushConfig();
        // xiaomi
        config.xmAppId = "2882303761520293137";
        config.xmAppKey = "5202029346137";
        config.xmCertificateName = "medical_XIAOMI";
        // honor
        config.honorCertificateName = "medical_HONOR";
       //  huawei
        config.hwAppId = "110130279";
        config.hwCertificateName = "medical_HUAWEI";
        // vivo
        config.vivoCertificateName = "medical_VIVO";
        // oppo
        config.oppoAppId = "31510743";
        config.oppoAppKey = "09b1e5216a6241929b74f786e31cdffd";
        config.oppoAppSercet = "4f6b4dfc0b7d44a195194487f9230bd8";
        config.oppoCertificateName = "medical_OPPO";
        return config;
    }
}
